/**
 * @file thread_management.c
 * @brief 
 * 
 * @author dalin (dalin@robot.com)
 * @version 1.0
 * @date 2023-05-08
 * 
 * @copyright Copyright (c) 2023  OpenSir Tech.co, Ltd
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-05-08 <td>1.0     <td>dalin     <td>内容
 * </table>
 */

#include "FreeRTOS.h"
#include "cmsis_os.h"
#include "task.h"
#include "main.h"
#include "mlog.h"


#include "thread_control.h"
#include "thread_communication.h"
#include "thread_display.h"
#include "thread_exception.h"
#include "thread_idle.h"
#include "thread_btest.h"
#include "thread_config.h"

#define THREAD_IDLE_SIZE		    128 * 2
#define THREAD_BTEST_SIZE		    128 * 1
#define THREAD_EXECPTION_SIZE		128 * 1
#define THREAD_DISPLAY_SIZE		    128 * 1
#define THREAD_COMMUN_SIZE		    128 * 4
#define THREAD_CONTROL_SIZE		    128 * 4


/* 任务列表ID */
enum{
    THREAD_ID_CONTROL,
    THREAD_ID_COMMUN,
    THREAD_ID_DISPLAY,
    THREAD_ID_EXECPTION,
    THREAD_ID_BTEST,

    THREAD_ID_IDLE,
    THREAD_ID_MAX
}threadID_e;

osThreadId TaskHandle[THREAD_ID_MAX];


/**
 * @brief 应用层任务创建
 * 
 * 
 */ 
void thread_init(void)
{
    int index;

    taskENTER_CRITICAL();

    /* 控制任务 */
#ifdef USE_CONTROL_THREAD
    index = THREAD_ID_CONTROL;
    osThreadDef(ControlTask, control_thread, osPriorityHigh, 0, THREAD_CONTROL_SIZE);
    TaskHandle[index] = osThreadCreate(osThread(ControlTask), NULL);
 #endif 

 #ifdef USE_COMMUN_THREAD
    /* 主通讯任务 */
    index = THREAD_ID_COMMUN;
    osThreadDef(CommunTask, commun_thread, osPriorityHigh, 0, THREAD_COMMUN_SIZE);
    TaskHandle[index] = osThreadCreate(osThread(CommunTask), NULL);
 #endif

#ifdef USE_DISPLAY_THREAD
    /* 显示及可视化任务 */
   index = THREAD_ID_DISPLAY;
   osThreadDef(DisplayTask, display_thread, osPriorityAboveNormal, 0, THREAD_DISPLAY_SIZE);
   TaskHandle[index] = osThreadCreate(osThread(DisplayTask), NULL);
 #endif 


#ifdef USE_EXECPTION_THREAD
    /* 异常处理任务*/
    index = THREAD_ID_EXECPTION;
    osThreadDef(ExecptionTask, exception_thread, osPriorityAboveNormal, 0, THREAD_EXECPTION_SIZE);
    TaskHandle[index] = osThreadCreate(osThread(ExecptionTask), NULL);
#endif  

#ifdef  USE_BTEST_THREAD
    /* 板级测试任务 */
    index = THREAD_ID_BTEST;
    osThreadDef(BtestTask, btest_thread, osPriorityHigh, 0, THREAD_BTEST_SIZE);
    TaskHandle[index] = osThreadCreate(osThread(BtestTask), NULL);
#endif 

#ifdef USE_IDLE_THREAD
    /* 空闲任务 */
    index = THREAD_ID_IDLE;
    osThreadDef(LowTask, idle_thread, osPriorityAboveNormal, 0, THREAD_IDLE_SIZE);
    TaskHandle[index] = osThreadCreate(osThread(LowTask), NULL);
#endif 

	taskEXIT_CRITICAL();
}

